package com.hy.controller;


import com.google.protobuf.ServiceException;
import com.hy.AppHttpCodeEnum;
import com.hy.Result.ResponseResult;
import com.hy.medicine.evaluation.dto.EvaluationReplyDTO;
import com.hy.medicine.evaluation.dto.MedicineEvaluationDTO;
import com.hy.medicine.evaluation.dto.MedicineEvaluationQuery;
import com.hy.medicine.evaluation.vo.EvaluationReplyVO;
import com.hy.medicine.evaluation.vo.MedicineEvaluationVO;
import com.hy.service.MedicineEvaluationService;
import com.hy.until.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陌寒    Lenovo
 * @date 2025/3/16下午10:30
 * @description 类描述
 *
 * 药品评价
 */
@Slf4j
@RestController
@RequestMapping("/api/medicine/evaluation")
@Api(tags = "药品评价接口")
public class MedicineEvaluationController {

    @Autowired
    private MedicineEvaluationService evaluationService;

    @GetMapping("/evaluations/{medicineId}")
    @ApiOperation("获取药品评价列表")
    public ResponseResult getEvaluationList(
            @PathVariable Integer medicineId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            MedicineEvaluationQuery query = new MedicineEvaluationQuery();
            query.setMedicineId(medicineId);
            query.setPageNum(pageNum);
            query.setPageSize(pageSize);
            PageResult<MedicineEvaluationVO> result = evaluationService.getEvaluationList(query);
            return ResponseResult.okResult(result);
        } catch (Exception e) {
            log.error("获取评价列表失败", e);
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR, "获取评价列表失败");
        }
    }

    @PostMapping("/evaluation")
    @ApiOperation("添加评价")
    public ResponseResult addEvaluation(@RequestBody @Validated MedicineEvaluationDTO dto) {
        try {
            // 参数校验
            if (dto.getMedicineId() == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "药品ID不能为空");
            }
            if (dto.getEvaluationScore() == null || dto.getEvaluationScore() < 1 || dto.getEvaluationScore() > 5) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "评分必须在1-5之间");
            }
            if (dto.getEvaluationContent() == null || dto.getEvaluationContent().trim().isEmpty()) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "评价内容不能为空");
            }

            evaluationService.addEvaluation(dto);
            return ResponseResult.okResult();
        } catch (Exception e) {
            log.error("添加评价失败: {}", e.getMessage(), e);
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR, "添加评价失败");
        }
    }

    @PostMapping("/evaluation/like/{evaluationId}")
    @ApiOperation("点赞")
    public ResponseResult toggleLike(@PathVariable Integer evaluationId) {
        try {
            evaluationService.toggleLike(evaluationId);
            return ResponseResult.okResult();
        } catch (ServiceException e) {
            log.error("点赞操作失败: {}", e.getMessage(), e);
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR, e.getMessage());
        } catch (Exception e) {
            log.error("点赞操作失败", e);
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR, "点赞操作失败");
        }
    }

    @PostMapping("/evaluation/reply")
    @ApiOperation("添加回复")
    public ResponseResult addReply(@RequestBody @Validated EvaluationReplyDTO dto) {
        try {
            // 参数校验
            if (dto.getEvaluationId() == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "评价ID不能为空");
            }
            if (dto.getReplyContent() == null || dto.getReplyContent().trim().isEmpty()) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "回复内容不能为空");
            }

            evaluationService.addReply(dto);
            return ResponseResult.okResult();
        } catch (Exception e) {
            log.error("添加回复失败: {}", e.getMessage(), e);
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR, "添加回复失败");
        }
    }

    @GetMapping("/evaluation/replies/{evaluationId}")
    @ApiOperation("获取回复列表")
    public ResponseResult getReplies(@PathVariable Integer evaluationId) {
        try {
            List<EvaluationReplyVO> replies = evaluationService.getReplies(evaluationId);
            return ResponseResult.okResult(replies);
        } catch (Exception e) {
            log.error("获取回复列表失败", e);
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR, "获取回复列表失败");
        }
    }
}
